include ApplicationHelper

class CustomersController < ApplicationController
  private

    def search_people
      order = "last_name"
      conditions_clauses = []
      conditions_options = []
      items_per_page = count_record_per_page :customers

      if params[:search_last_name] != ""
        conditions_clauses << "upper(last_name) like upper(?)"
        conditions_options << "%#{params[:search_last_name]}%"
      end
      conditions = [conditions_clauses.join(" AND "), conditions_options].flatten
      @customers = People.paginate :per_page => items_per_page,:page => params[:page], :conditions => conditions, :order => order
    end

    def search_institute
      order = "name"
      conditions_clauses = []
      conditions_options = []
      items_per_page = count_record_per_page :customers

      if params[:search_name] != ""
        conditions_clauses << "upper(name) like upper(?)"
        conditions_options << "%#{params[:search_name]}%"
      end
      conditions = [conditions_clauses.join(" AND "), conditions_options].flatten
      @customers = Institute.paginate :per_page => items_per_page,:page => params[:page], :conditions => conditions, :order => order      
    end

    def search_libraries
      order = "name"
      conditions_clauses = []
      conditions_options = []
      items_per_page = count_record_per_page :customers

      if params[:search_name] != ""
        conditions_clauses << "upper(name) like upper(?)"
        conditions_options << "%#{params[:search_name]}%"
      end
      conditions = [conditions_clauses.join(" AND "), conditions_options].flatten
      @customers = Library.paginate :per_page => items_per_page,:page => params[:page], :conditions => conditions, :order => order
    end

    def search_library_departments
      order = "name"
      conditions_clauses = []
      conditions_options = []
      items_per_page = count_record_per_page :customers

      if params[:search_name] != ""
        conditions_clauses << "upper(name) like upper(?)"
        conditions_options << "%#{params[:search_name]}%"
      end
      conditions = [conditions_clauses.join(" AND "), conditions_options].flatten
      @customers = LibraryDepartment.paginate :per_page => items_per_page,:page => params[:page], :conditions => conditions, :order => order
    end

    def search_bookshop
      order = "name"
      items_per_page = count_record_per_page :customers
      conditions_clauses = []
      conditions_options = []

      if params[:search_name] != ""
        conditions_clauses << "upper(name) like upper(?)"
        conditions_options << "%#{params[:search_name]}%"
      end
      conditions = [conditions_clauses.join(" AND "), conditions_options].flatten
      @customers = Bookshop.paginate :per_page => items_per_page,:page => params[:page], :conditions => conditions, :order => order
    end

    def search_bookshop_departments
      order = "name"
      items_per_page = count_record_per_page :customers
      conditions_clauses = []
      conditions_options = []

      if params[:search_name] != ""
        conditions_clauses << "upper(name) like upper(?)"
        conditions_options << "%#{params[:search_name]}%"
      end
      conditions = [conditions_clauses.join(" AND "), conditions_options].flatten
      @customers = BookshopDepartment.paginate :per_page => items_per_page,:page => params[:page], :conditions => conditions, :order => order
    end

    def search_publishers
      order = "name"
      items_per_page = count_record_per_page :customers
      conditions_clauses = []
      conditions_options = []

      if params[:search_name] != ""
        conditions_clauses << "upper(name) like upper(?)"
        conditions_options << "%#{params[:search_name]}%"
      end
      conditions = [conditions_clauses.join(" AND "), conditions_options].flatten
      @publishers = Publisher.paginate :per_page => items_per_page,:page => params[:page], :conditions => conditions, :order => order
    end

  public
    def selector_people_initial
      search_people
      render :partial=>'selector_people' , :layout=>false
    end

    def selector_people_list
      search_people
      render :partial=>'selector_people_list', :locals => {:search_last_name => params[:search_last_name]}, :layout=>false
    end

    def selector_people_list_from_paginate
        search_people
    end

    def selector_institutes_initial
      search_institute
      render :partial=>'selector_institutes' , :layout=>false
    end

    def selector_institutes_list
      search_institute
      render :partial=>'selector_institutes_list', :locals => {:search_name => params[:search_name]}, :layout=>false
    end

    def selector_institutes_list_from_paginate
        search_institute
    end

    def selector_libraries_initial
      search_libraries
      render :partial=>'selector_libraries' , :layout=>false
    end

    def selector_libraries_list
      search_libraries
      render :partial=>'selector_libraries_list', :locals => {:search_name => params[:search_name]}, :layout=>false
    end

    def selector_libraries_list_from_paginate
        search_libraries
    end

    def selector_library_departments_initial
      search_library_departments
      render :partial=>'selector_library_departments' , :layout=>false
    end

    def selector_library_departments_list
      search_library_departments
      render :partial=>'selector_library_departments_list', :locals => {:search_name => params[:search_name]}, :layout=>false
    end

    def selector_library_departments_list_from_paginate
        search_library_departments
    end

    def selector_bookshops_initial
      search_bookshop
      render :partial=>'selector_bookshops' , :layout=>false
    end

    def selector_bookshops_list
      search_bookshop      
      render :partial=>'selector_bookshops_list', :locals => {:search_name => params[:search_name]}, :layout=>false
    end

    def selector_bookshops_list_from_paginate
        search_bookshop
    end

    def selector_bookshop_departments_initial
      search_bookshop_departments
      render :partial=>'selector_bookshop_departments' , :layout=>false
    end

    def selector_bookshop_departments_list
      search_bookshop_departments
      render :partial=>'selector_bookshop_departments_list', :locals => {:search_name => params[:search_name]}, :layout=>false
    end

    def selector_bookshop_departments_list_from_paginate
        search_bookshop_departments
    end

    def selector_publishers_initial
      search_publishers
      render :partial=>'publishers/selector_publishers' , :layout=>false
    end

    def selector_publishers_list
      search_publishers
      render :partial=>'publishers/selector_publishers_list', :locals => {:search_name => params[:search_name]}, :layout=>false
    end

    def selector_publishers_list_from_paginate
        search_publishers
        render 'publishers/selector_publishers_list_from_paginate'
    end

    def selector_list      
      if params[:search_type] == 'People'
        selector_people_initial
      end
      if params[:search_type] == 'Institute'
        selector_institutes_initial
      end
      if params[:search_type] == 'Library'
        selector_libraries_initial
      end
      if params[:search_type] == 'LibraryDepartment'
        selector_library_departments_initial
      end
      if params[:search_type] == 'Bookshop'
        selector_bookshops_initial
      end
      if params[:search_type] == 'BookshopDepartment'
        selector_bookshop_departments_initial
      end
      if params[:search_type] == 'Publisher'
        selector_publishers_initial
      end
      if params[:search_type] == ''
        render :partial=>'selector_empty_list' , :layout=>false
      end
    end
end
